package com.xindacloud.sms.console.system.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.ModifySmsAgentDTO;
import com.xindacloud.sms.console.system.dto.SaveSmsAgentDTO;
import com.xindacloud.sms.console.system.dto.SmsAgentDTO;
import com.xindacloud.sms.console.system.entity.SmsAgent;
import com.xindacloud.sms.console.system.entity.SmsPricing;
import com.xindacloud.sms.console.system.entity.SmsUserAllowOperator;
import com.xindacloud.sms.console.system.mapper.SmsAgentMapper;
import com.xindacloud.sms.console.system.mapper.SmsPricingMapper;
import com.xindacloud.sms.console.system.mapper.SmsUserAllowOperatorMapper;
import com.xindacloud.sms.console.system.service.ISmsAgentService;
import com.xindacloud.sms.console.system.service.ISmsUserAllowOperatorService;

/**
 * <p>
 * 短信通道代理商 服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */
@Service
public class SmsAgentServiceImpl extends ServiceImpl<SmsAgentMapper, SmsAgent> implements ISmsAgentService {

    @Override
    public Result getSmsPlatformList(SmsAgentDTO smsAgentDTO) {
        IPage<SmsAgent> page = new Page<>(smsAgentDTO.getPageNum(), smsAgentDTO.getPageSize());
        QueryWrapper<SmsAgent> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(smsAgentDTO.getAgentName()), "agent_name", smsAgentDTO.getAgentName());
        queryWrapper.orderByDesc("status");
        return ResultGenerator.genSuccessResult(this.page(page, queryWrapper));
    }

    @Override
    public SmsAgent addSmsAgent(SaveSmsAgentDTO saveSmsAgentDTO) {
        SmsAgent smsAgent = new SmsAgent();
        BeanUtils.copyProperties(saveSmsAgentDTO, smsAgent);
        save(smsAgent);
        return smsAgent;
    }

    @Override
    public Boolean modifySmsAgent(ModifySmsAgentDTO modifySmsAgentDTO) {

        return lambdaUpdate()
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getAgentName()), SmsAgent::getAgentName, modifySmsAgentDTO.getAgentName())
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getAgentAliasName()), SmsAgent::getAgentAliasName, modifySmsAgentDTO.getAgentAliasName())
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getApiKey()), SmsAgent::getApiKey, modifySmsAgentDTO.getApiKey())
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getApiSecret()), SmsAgent::getApiSecret, modifySmsAgentDTO.getApiSecret())
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getApiUrl()), SmsAgent::getApiUrl, modifySmsAgentDTO.getApiUrl())
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getCallBackUrl()), SmsAgent::getCallBackUrl, modifySmsAgentDTO.getCallBackUrl())
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getGroupId()), SmsAgent::getGroupId, modifySmsAgentDTO.getGroupId())
                .set(StringUtils.isNotEmpty(modifySmsAgentDTO.getSenderId()), SmsAgent::getSenderId, modifySmsAgentDTO.getSenderId())
                .set(Objects.nonNull(modifySmsAgentDTO.getDeliveryReportReq()), SmsAgent::getDeliveryReportReq, modifySmsAgentDTO.getDeliveryReportReq())
                .set(Objects.nonNull(modifySmsAgentDTO.getStatus()), SmsAgent::getStatus, modifySmsAgentDTO.getStatus())
                .set(Objects.nonNull(modifySmsAgentDTO.getIsDefault()), SmsAgent::getIsDefault, modifySmsAgentDTO.getIsDefault())
                .eq(SmsAgent::getAgentId, modifySmsAgentDTO.getAgentId())
                .update();
    }

    @Override
    public Boolean updateAgentStatus(List<String> agentGroupIds, Boolean status) {
        return lambdaUpdate()
                .set(SmsAgent::getStatus, status)
                .in(SmsAgent::getGroupId, agentGroupIds)
                .update();
    }

    @Override
    public List<SmsAgent> querySmsAgentByGroupId(List<String> groupIds) {
        return lambdaQuery()
                .in(SmsAgent::getGroupId, groupIds)
                .list();
    }
    @Autowired
    ISmsUserAllowOperatorService userAllowOperatorService;
	@Override
	public Result getALLSmsAgentList(SmsAgentDTO smsAgentDTO) {
		Integer roleLevel=userAllowOperatorService.getRole(smsAgentDTO.getUserId());
		if(roleLevel.equals(1)) {
			List<SmsAgent> agentList=this.list();
			for(SmsAgent smsAgent:agentList) {
				smsAgent.setAgentName(smsAgent.getAgentAliasName());
			}
			return	ResultGenerator.genSuccessResult(agentList);
		}
		return ResultGenerator.genSuccessResult(this.list());
	}

	@Autowired
	private  SmsPricingMapper smsPricingMapper;
	@Autowired
	private  SmsUserAllowOperatorMapper smsUserAllowOperatorMapper;
	
	@Override
	public Result deleteAgents(SmsAgentDTO smsAgentDTO) {
		List<String> agentIds=smsAgentDTO.getAgentIds();
		if(smsAgentDTO.getAgentIds()!=null) {
		for(String agentId:agentIds) {
		QueryWrapper<SmsPricing> spWrapper = new QueryWrapper<>();
		spWrapper.eq("agent_id", agentId);
		smsPricingMapper.delete(spWrapper);
		
		QueryWrapper<SmsUserAllowOperator> saoWrapper = new QueryWrapper<>();
		saoWrapper.eq("agent_id", agentId);
		smsUserAllowOperatorMapper.delete(saoWrapper);
		}
		}
		if(this.removeByIds(agentIds)) {
			return ResultGenerator.genSuccessResult("删除成功！" );
		}else {
			return ResultGenerator.genServerFailResult("删除失败！请联系管理员");
		}
	}

	@Autowired
	ISmsUserAllowOperatorService smsUserAllowOperatorService;
	
	@Override
	public Result updateAgentStatus(SmsAgentDTO smsAgentDTO) {
		
		boolean result= lambdaUpdate()
                .set(SmsAgent::getStatus, smsAgentDTO.getStatus())
                .in(SmsAgent::getAgentId, smsAgentDTO.getAgentIds())
                .update();
		boolean uaoResult=smsUserAllowOperatorService.updateStatusByAgentIds(smsAgentDTO);
		if(!result) {
			return ResultGenerator.genServerFailResult("修改代理商状态失败！请联系管理员");
		}else if (!uaoResult) {
			return ResultGenerator.genServerFailResult("修改用户下的代理商状态失败！请联系管理员");
		}else {
			return ResultGenerator.genSuccessResult("修改成功！" );
		}
		
	}
    
}
